package com.yw.pms.controller;

import java.io.File;
import java.io.FileInputStream;
import java.util.Date;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Controller;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.yw.pms.utils.CommonResp;

@Controller
@RequestMapping("/common")
@PropertySource("classpath:app.properties")
public class CommonController {
	
	private final static Logger logger = LoggerFactory.getLogger(CommonController.class);
	
	@Value("${pic.upload.path}")
	private String picUploadPath;
	
	@RequestMapping("/picUpload")
	@ResponseBody
	CommonResp<String> picUpload(MultipartFile pictureFile) {
		logger.info("文件上传");
		CommonResp<String> commonResp = new CommonResp<String>();
		String fileName = pictureFile.getOriginalFilename();
		fileName = DateFormatUtils.format(new Date(), "yyyyMMddHHmmss") + fileName.substring(fileName.indexOf('.'));
		logger.info("file name: " + fileName);
		File targetFile = new File(picUploadPath, fileName);
		if (!targetFile.exists()) {
			targetFile.mkdirs();
		}
		try {
			pictureFile.transferTo(targetFile);
		} catch (Exception e) {
			logger.error("上传文件失败", e);
			commonResp.fail();
			commonResp.setMsg("保存图片异常");
			return commonResp;
		}
		String fileUrl = picUploadPath + File.separator + fileName;
		commonResp.success();
		commonResp.setData(StringUtils.replace(fileUrl, "\\", "\\\\"));
		return commonResp;
	}
	
	@RequestMapping("/getPic")
	public void getPic(HttpServletResponse response, String path) {
		
		response.setContentType("application/octet-stream;charset=UTF-8");
		try {
			FileInputStream in = new FileInputStream(path);
			int i = in.available();
			byte[] data = new byte[i];
			in.read(data);
			IOUtils.closeQuietly(in);
			ServletOutputStream outputStream = response.getOutputStream();
			IOUtils.write(data, outputStream);
			outputStream.flush();
			IOUtils.closeQuietly(outputStream);
		} catch (Exception e) {
			logger.error("查找图片异常"+path, e);
			try {
				File file = ResourceUtils.getFile("classpath:static/image/null.jpg");
				FileInputStream in = new FileInputStream(file);
				int i = in.available();
				byte[] data = new byte[i];
				in.read(data);
				IOUtils.closeQuietly(in);
				ServletOutputStream outputStream = response.getOutputStream();
				IOUtils.write(data, outputStream);
				outputStream.flush();
				IOUtils.closeQuietly(outputStream);
			} catch (Exception e1) {
				e1.printStackTrace();
			}
		}
	}
}
